import BasicController from './../../BasicController'
import ErrorHandlerCode from './../../../util/ErrorHandlerCode'
import CustomerGroupModel from '../../../model/basic/logistics/CustomerGroupModel'
class CustomerGroupController extends BasicController{
    insert(req, res){
        if(!super.isEmptyParameter(req.body.customer_group_name)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'customer_group_name 为必选参数'
            })
            return;
        }
        if(!super.isEmptyParameter(req.body.customer_group_type)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'customer_group_type 为必选参数'
            })
            return;
        }
        CustomerGroupModel.insert(req.body).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    updateRecord(req, res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        if(!super.isEmptyParameter(req.body.customer_group_name)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'customer_group_name 为必选参数'
            })
            return;
        }
        if(!super.isEmptyParameter(req.body.customer_group_type)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'customer_group_type 为必选参数'
            })
            return;
        }
        CustomerGroupModel.update(req.body).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    deleteById(req, res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        CustomerGroupModel.delete(req.body.id).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findById(req, res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        CustomerGroupModel.findById(req.body.id).then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findAll(req, res){
        CustomerGroupModel.findAll().then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findByPage(req, res){
        req.body.page_size = req.body.page_size || 10;
        req.body.page_no = req.body.page_no || 1;
        CustomerGroupModel.findByPage(req.body).then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}
module.exports = new CustomerGroupController();